import {Pipe, PipeTransform} from '@angular/core';

@Pipe({
  name: 'countDown'
})
export class CountDownPipe implements PipeTransform {

  transform(value: number, args?: any): any {
    // 计算天，小时，分，秒的除数
    const bySecond = 1000;
    const byMinute = bySecond * 60;
    const byHour = byMinute * 60;
    const byDay = byHour * 24;
    // 待拼接倒计时字符串
    let countDown = '';
    // 计算剩余天数
    const day = Math.floor(value / byDay);
    countDown += day > 0 ? day + '天' : '';
    let Remainder = value % byDay;
    // 计算剩余小时
    const hour = Math.floor(Remainder / byHour);
    countDown += hour < 10 ? '0' + hour : hour;
    countDown += '小时';
    Remainder = Remainder % byHour;
    // 计算剩余分钟数
    const minute = Math.floor(Remainder / byMinute);
    countDown += minute < 10 ? '0' + minute : minute;
    countDown += '分钟';
    Remainder = Remainder % byMinute;
    // 计算剩余秒数
    const second = Math.floor(Remainder / bySecond);
    countDown += second < 10 ? '0' + second : second;
    countDown += '秒';

    return countDown;
  }

}
